/*
    Copyright (c) 2008-2009, Prashanta Shrestha All Rights Reserved.
    Available under GNU Lesser General Public License >= 3.0 as published by the Free Software Foundation.
    see <http://www.gnu.org/licenses/>
*/
/*-------------------------------------------------------------------------------------
  CLASS : qDojo.dijix.Menu
  ==================================
  - Navigation button widget 
  -------------------------------------------------------------------------------------*/
dojo.provide("qDojo.dijix.Menu");

dojo.require("qDojo.core.AjaxHandler");
dojo.require("qDojo.dijix.MenuItem");
dojo.require("dijit._Container");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dojo.data.ItemFileReadStore");

dojo.declare("qDojo.dijix.Menu", [ dijit._Container , dijit._Widget ,  dijit._Templated , qDojo.core.AjaxHandler ] , {

		templateString: " <div style=\"background:#CDDCF3;height:100%;\" dojoAttachPoint=\"outerNode\">" + 
							"<div dojoAttachPoint=\"containerNode\"></div>"+
						" </div>",
		viewCanvas : "",		
		file: "",
		width:"",				
		justClickedChild : null,
		// ======================================================================
		//	Post create widget 
		// ----------------------------------------------------------------------
		postCreate: function() 
		{		
			this.inherited(arguments);		
			this.domNode.style.width = this.width; 					
			if(this.file)
			{
				var filePath = dojo.moduleUrl("dojo","../../../") + this.file;			
				var store = new dojo.data.ItemFileReadStore({ url: filePath }); 	
				var menuItem = new Array();				
				var _this = this;
				var gotList  = function(items, request)
				{
				    dojo.forEach(items, function(i)
				    { 
					   var label  = typeof store.getValue(i, "label") == "undefined"? "" : store.getValue(i, "label");
					   var icon   = typeof store.getValue(i, "iconClass") == "undefined"? "" : store.getValue(i, "iconClass");
					   var event = typeof store.getValue(i, "event") == "undefined"? "" : store.getValue(i, "event");																						
					   var item = new qDojo.dijix.MenuItem({ label:label , iconClass:icon , event:event });
					   _this.addChild(item);
					});
				}
				var gotError = function(error, request){console.log("The request to the store failed. " +  error);}				
				store.fetch({onComplete: gotList,onError: gotError});				
			}			
		},
		// ======================================================================
		//	Some child is clicked
		// ----------------------------------------------------------------------
		childClicked: function(child)
		{		    
			this.justClickedChild = child.id;
			//remove this... 
			document.title = ( typeof _appTitle == "undefined" )? "" : _appTitle + " : " + dijit.byId(child).label;
			if(child.event)
                this.exec(dojo.fromJson(child.event));		
            else
                dijit.byId(this.justClickedChild).activate();                    			                          
		},
		// ======================================================================
		//	Render View
		// ----------------------------------------------------------------------		
		renderView: function(payload,event,responseType)
		{						
			dijit.byId(this.justClickedChild).activate();						
			dijit.byId(this.viewCanvas).destroyDescendants();
			dijit.byId(this.viewCanvas).renderView(payload,event,responseType);									
		},
		// ======================================================================
		//	Deactivate currently active child
		// ----------------------------------------------------------------------
		deactivateChildren : function()
		{			
			dojo.forEach(this.getChildren(), function(child){
				if(child.active) child.deactivate();
			});
		},
        // ======================================================================
        //  Active child 
        // ----------------------------------------------------------------------
        activateChildren : function(id)
        {           
            dojo.forEach(this.getChildren(), function(child){
                if(child.id==id) child.activate();
            });
        }
});


